﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Krile.Kernel.Data;

namespace Twinkle.Searchs
{
    /// <summary>
    /// Filter by following users<br />
    /// (only 
    /// </summary>
    public class SearchTwitterFollowings : SingleSearchData, ISearchTwitterAccountTarget
    {

        public string LinkingUserId { get; set; }

        public override void AcceptArgument(object[] arguments)
        {
            AssertTypeof(arguments, typeof(string));
            LinkingUserId = (string)arguments[0];
        }

        public override string Method
        {
            get { return "FollowingBy"; }
        }

        public override string Identification
        {
            get { return "Twitter_Twinkle_User_FollowedBy"; }
        }

        protected override bool IsMatchedInner(Status status)
        {
            foreach (var a in Kernel.Config.Accounts)
            {
                if (a.UserId == LinkingUserId)
                {
                    if (a.followingUsers != null && status.User != null &&
                        a.followingUsers.Contains(status.User.Id))
                        return true;
                }
            }
            return false;
        }

        public override string Description
        {
            get { return Lang.General.TwitterFollowingsDescription; }
        }

        public override string Name
        {
            get { return Lang.General.TwitterFollowingsName; }
        }

        public override string Class
        {
            get { return "Twitter"; }
        }

        protected override string WriteArguments()
        {
            return "\"" + this.LinkingUserId + "\"";
        }

        public override void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteElementString("LinkingUserId", LinkingUserId);
        }

        public override void ReadXml(System.Xml.XmlReader reader)
        {
            if (reader.Name != "LinkingUserId")
                reader.ReadToDescendant("LinkingUserId");
            LinkingUserId = reader.ReadElementString("LinkingUserId");
        }

        public override Krile.Kernel.Control.SDataArgSetUI GetArgSetUI()
        {
            return new Controls.UserSet(this);
        }

        public override object Clone()
        {
            return new SearchTwitterFollowings() { LinkingUserId = LinkingUserId, Exclude = Exclude };
        }

        #region ISearchTwitterAccountTarget メンバー

        public string TargetAccountId
        {
            get
            {
                return this.LinkingUserId;
            }
            set
            {
                this.LinkingUserId = value;
            }
        }

        #endregion
    }
}
